//search.js
//获取应用实例
var app = getApp()
Page({
  data: {
    userInfo: {},
    contact_num:'',
    price_list: [
      {
        price_id: "1",
        price_time: "一月",
        price_value: "200元/棵",
      },
      {
        price_id: "2",
        price_time: "二月",
        price_value: "300元/棵",
      }]
  },
  //事件处理函数
  goAdvice: function () {
    wx.navigateTo({
      url: '/pages/advice/advice',
    })
  },
  goAdvice2: function () {
    wx.navigateTo({
      url: '/pages/advice-list/advice-list',
    })
  },
  memberInfo:function() {
    wx.navigateTo({
      url: '/pages/member-info/member-info',
    })
  },
  assessPage: function() {
    wx.navigateTo({
      url: '/pages/assess-page/assess-page',
    })
  },
  ticketInfo: function() {
    wx.navigateTo({
      url: '/pages/ticket-info/ticket-info',
    })
  },
  assessLog: function() {
    wx.navigateTo({
      url: '/pages/assess-log/assess-log',
    })
  },
  login_out: function () {
    wx.request({
      url: app.globalData.base_url,
      header: {
        'Content-Type': 'application/json;charset=UTF-8;'
      },
      method: "POST",
      data: {
        "ActionType": "event_customer_logout",
        "DataDictionary": {
          "CUSTOMER_ID": wx.getStorageSync('user_id'),
        }
      },
      success: function (res) {
        wx.redirectTo({
          url: '/pages/index/index',
        })
      }
    })
  },
  onLoad: function (options) {

    wx.showShareMenu({
      withShareTicket: true
    })
    
    var that = this
    var instance = this;
    
    wx.request({
      url: app.globalData.base_url + "Contact_num/getContactNumber",
      // port: app.globalData.base_port,
      header: {
        'Content-Type': 'application/json;charset=UTF-8;'
      },
      method: "POST",
      data: {
        "CUSTOMER_ID": wx.getStorageSync('user_id')
      },
      success: function (res) {
        that.setData({
          contact_num: res.data.data.CONTACT_NUMBER
        });
      }
    })    
  },

   //分享
  onShareAppMessage: function (res) {
    let that = this
    return {
      title: '找苗木网',
      path: '/pages/login1/login1',
      imageUrl: '../../source/icon/banner.png',
      success: function (res) {
        console.log(res.shareTickets)
        //getSystemInfo是为了获取当前设备信息，判断是android还是ios，如果是android
        //还需要调用wx.getShareInfo()，只有当成功回调才是转发群，ios就只需判断shareTickets
        //获取用户设备信息
        wx.getSystemInfo({
          success: function (d) {
            console.log(d);
            if (res.shareTickets != undefined) {
              console.log("分享的是群");
              wx.getShareInfo({
                shareTicket: res.shareTickets[0],
                success: function (res) {
                  //分享到群之后你要做的事情
                  wx.request({
                    url: app.globalData.base_url + "Customer/grantTicketByGroup",
                    header: {
                      'Content-Type': 'application/json;charset=UTF-8;'
                    },
                    method: "POST",
                    data: {
                      "CUSTOMER_ID": wx.getStorageSync('user_id'),
                    },
                    complete: function (res) {
                      that.setData({

                      });
                      if (res == null || res.data == null) {
                        console.error('网络请求失败');
                        return;
                      }
                    },
                    success: function (res) {
                      if (res.data.code === "200") {
                        wx.showModal({
                          title: '提示',
                          content: '赠送您一张估价券哦，有效时间24小时',
                          success: function (res) {
                            if (res.confirm) {
                              console.log('用户点击确定')
                            } else if (res.cancel) {
                              console.log('用户点击取消')
                            }
                          }
                        })
                      } else if (res.data.code === "203") {
                        var maxGrantCount = res.data.data;
                        wx.showModal({
                          title: '提示',
                          content: '分享到群每天最多赠送 ' + maxGrantCount + ' 张您已达到上限',
                          success: function (res) {
                            if (res.confirm) {
                              console.log('用户点击确定')
                            } else if (res.cancel) {
                              console.log('用户点击取消')
                            }
                          }
                        })
                      }
                    }
                  })
                }
              })
            } else {//分享到个人要做的事情，我给的是一个提示
              console.log("分享的是个人");
              wx.showModal({
                title: '提示',
                content: '分享到群赠送估价券哦',
                success: function (res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })
            }
          },
          fail: function (res) {

          }
        })
      }

    }
  },
})
